package com.tongfusoft.web.core.action;

import java.util.List;
import java.util.Map;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.lang.StringUtils;

import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.web.core.Constant;
import com.tongfusoft.web.core.data.BaseData;
import com.tongfusoft.web.core.service.IManagementService;

@SuppressWarnings("unchecked")
public abstract class ManagementBaseAction  extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1805342298940477022L;
	
	/**
	 * 操作符
	 */
	protected String oper;
	
	/**
	 * 实体bean名称，包名（最后一层）+名称
	 */
	protected String beanName;
	
	/**
	 * 执行结果
	 */
	protected boolean result;
	
	/**
	 * 返回的提示信息
	 */
	protected String message;
	
	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	private IManagementService managementService;

	
	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	/**
	 * 操作单条记录
	 */
	public String execute(){
		if(StringUtils.isNotEmpty(this.getOper())){
			if(this.getOper().equals(Constant.OPER_ADD)){
				return this.add();
			}else if(this.getOper().equals(Constant.OPER_EDIT)){
				return this.update();
			}else if(this.getOper().equals(Constant.OPER_DEL)){
				return this.remove();
			}else{
				result = false;
				message = super.getText("common.prompt.validoper");
			}
		}else{
			result = false;
			message = super.getText("common.prompt.fail");
		}
		
		return SUCCESS;
	}
	
	/**
	 * 操作多条记录
	 * @return
	 */
	public String executeList(){
		if(StringUtils.isNotEmpty(this.getOper())){
			if(this.getOper().equals(Constant.OPER_ADD)){
				return this.addList();
			}else if(this.getOper().equals(Constant.OPER_DEL)){
				return this.removeList();
			}else{
				result = false;
				message = super.getText("common.prompt.validoper");
			}
		}else{
			result = false;
			message = super.getText("common.prompt.fail");
		}
		
		return SUCCESS;
	}
	
	/**
	 * 将传入的参数转换为需要的对象
	 * @return
	 */
	public abstract BaseData convertParam2Obj(); 
	
	/**
	 * 将传入的参数转换为需要的对象列表
	 * @return
	 */
	public abstract List<BaseData> convertParam2List();
	
	/**
	 * 新增
	 * @return
	 */
	public String add(){
		try{
			managementService.add(this.convertParam2Obj());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 新增列表
	 * @return
	 */
	public String addList(){
		try{
			managementService.add(this.convertParam2List());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 更新
	 * @return
	 */
	public String update(){
		try{
			managementService.update(this.convertParam2Obj());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		try{
			managementService.remove(this.convertParam2Obj());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	public String removeList(){
		try{
			managementService.remove(this.convertParam2List());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 批量更新
	 * @return
	 */
	public String updateList(){
		try{
			managementService.update(this.convertParam2List());
			result = true;
			message = super.getText("common.prompt.success");
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result = false;
			
			if(e instanceof ServiceException && StringUtils.isNotEmpty(((ServiceException)e).getErrMsgCode())){
				message = super.getText(((ServiceException)e).getErrMsgCode());
			}else{
				message = super.getText("common.prompt.fail");
			}
		}
		
		return SUCCESS;
	}
}
